import Vue from 'vue'
import VueRouter from 'vue-router'
import Container from '@/components/container/Container.vue'
import Welcome from '@/views/welcome/Index.vue'
import Login from '@/views/login/Index.vue'
import Register from '@/views/register/Index.vue'
import GoodsList from '@/views/goods-manage/goods-list/Index.vue'
import OrderList from '@/views/order-manage/order-list/Index.vue'
import EveryProvince from '@/views/sale-situation/every-province/Index.vue'
import EveryFruit from '@/views/sale-situation/every-fruit/Index.vue'
import RouterView from '@/components/router-view/RouterView.vue'

// 解决重复点击菜单报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

export const routes = [
  {
    path: '/',
    name: 'index',
    meta: { title: '首页' },
    component: Container,
    redirect: '/welcome',
    children: [
      {
        path: 'welcome',
        name: 'welcome',
        component: Welcome,
        meta: { title: '欢迎' }
      },
      {
        path: 'goods-manage',
        name: 'goods-manage',
        component: RouterView,
        redirect: 'goods-manage/goods-list',
        meta: { title: '商品管理' },
        children: [
          {
            path: 'goods-list',
            name: 'goods-list',
            component: GoodsList,
            meta: { title: '商品列表' }
          }
        ]
      },
      {
        path: 'order-manage',
        name: 'order-manage',
        component: RouterView,
        redirect: 'order-manage/order-list',
        meta: { title: '订单管理' },
        children: [
          {
            path: 'order-list',
            name: 'order-list',
            component: OrderList,
            meta: { title: '订单列表' }
          }
        ]
      },
      {
        path: 'sale-situation',
        name: 'sale-situation',
        component: RouterView,
        redirect: 'sale-situation/every-province',
        meta: { title: '销售情况' },
        children: [
          {
            path: 'every-province',
            name: 'every-province',
            component: EveryProvince,
            meta: { title: '各个省份' }
          },
          {
            path: 'every-fruit',
            name: 'every-fruit',
            component: EveryFruit,
            meta: { title: '各种水果' }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录' },
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    meta: { title: '注册' },
    component: Register
  },
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name === 'login' || to.name === 'register') {
    window.sessionStorage.removeItem('token')
    window.sessionStorage.removeItem('username')
    next()
  } else {
    const token = window.sessionStorage.getItem('token')
    !token ? next({ path: '/login' }) : next()
  }
})

export default router
